var WindEntities = new Object();

WindEntities.createTableModel = function(config) {
	
	var cmd = new Object();
	cmd.operation = "datastore.list"
	cmd.type = config.type;
	cmd.sort = config.sort;
	cmd.filters = config.filters;
	
	var result = WindData.execute(cmd);	
	var tableModel = WindTable.createModel();
	tableModel.setTitle(config.tableTitle);
	tableModel.clearButtons();
	{
		var button = tableModel.getToolbar().createButton();
		button.setTitle("New");
		
		button.onClick = function() {
			var dialogModel = WindEntities.createDetailDialog("insert", null, config);
				dialogModel.onClose = function(code) {
				if(code=="inserted") {
					WindEntities.updateTable(tableModel, config);
				}
			}
			var dialogView = WindDialog.createView(dialogModel);
			config.configureDialogView(dialogView, null);
			dialogView.open();
		}
	}
	{
		var button = tableModel.getToolbar().createButton();
		button.setTitle("Edit");
		
		button.onClick = function() {
			var selected = tableModel.getSelectedRows();
			if(selected.length==1) {
				key = selected[0].getCell(0);
				
				var cmd = new Object();
				cmd.operation = "datastore.list"
				cmd.key = key;
				
				var response = WindData.execute(cmd)
				var entity = response.entity;
				
				var dialogModel = WindEntities.createDetailDialog("update", entity, config);
				dialogModel.onClose = function(code) {
					if(code=="updated") {
						WindEntities.updateTable(tableModel, config);
					}
				}
				var dialogView = WindDialog.createView(dialogModel);
				
				dialogView.keyText = WindText.createView();
				dialogView.keyText.setValue(entity.key);
				dialogView.addView(dialogView.keyText);
				dialogView.keyText.hide();
				
				config.configureDialogView(dialogView, entity);
				
				dialogView.open();
			}			
		}
	}
	{
		var button = tableModel.getToolbar().createButton();
		button.setTitle("Delete");
		
		button.onClick = function() {
			var selected = tableModel.getSelectedRows();
			if(selected.length>0) {
				var keys = "";
				for(var i=0; i<selected.length; i++) {
					keys += selected[i].getCell(0);
					if(i<(selected.length-1)) {
						keys += ",";
					}
				}
				var cmd = new Object();
				cmd.operation = "datastore.delete"
				cmd.keys = keys;
				var response = WindData.execute(cmd);
				
				if(response.errors.length==0) {
					tableModel = WindEntities.updateTable(tableModel, config);
					var tableView = tableModel.getView();
					tableView.addInfo("Deleted finished succsesfully")
				} else {
					for(var i=0; i<response.errors.length; i++) {
						tableModel.getView().addError(response.errors[i]);
					}
				}
			}
		}
	}
	
	tableModel.clearColumns();
	{
		var column = new WindTableColumnModel();
		column.setTitle("Key");
		column.setVisible(false);
		tableModel.addColumn(column);
	}
	config.addTableColumns(tableModel);

	tableModel.clearRows();
	for(var i=0; i<result.rows.length; i++) {
		var row = new WindTableRowModel();

		var key = result.rows[i].key;
		row.addCell(key);
		
		config.addTableRowCellsFromEntity(row, result.rows[i]);
		
		tableModel.addRow(row);
	}
	
	return tableModel;
}

WindEntities.createTableView = function(tableModel) {
	if(tableModel==null) {
		tableModel = WindEntities.createTableModel();
	}
	var tableView = WindTable.createView(tableModel);
	return tableView;
}

WindEntities.createTableViewFromConfig = function(config) {
	var tableModel = WindEntities.createTableModel(config);
	var tableView = WindTable.createView(tableModel);
	return tableView;
}

WindEntities.updateTable = function(tableModel, config) {
	var oldView = tableModel.getView();
	var newModel = WindEntities.createTableModel(config);
	var newView = WindEntities.createTableView(newModel);
	WindWindow.updateView(oldView, newView);
	return newModel;
} 

WindEntities.createDetailDialog = function(mode, entity, config) {
	var dialogModel = WindDialog.createModel();

	if(mode==null) {
		mode = "insert";
	}
	
	if(mode=="insert") {
		dialogModel.setTitle(config.dialogInsertTitle);
	} else if(mode=="update") {
		dialogModel.setTitle(config.dialogUpdateTitle);
	}
	
	if(mode=="insert") {
		var button = dialogModel.getToolbar().createButton();
		button.setTitle("Insert");
		button.onClick = function() {
			var cmd = new Object();
			cmd.operation = "datastore.insert";
			cmd.entity = new Object();
			cmd.entity.type = config.type;

			config.fillEntityFromDialog(dialogModel.getView(), cmd.entity);
		
			var result = WindData.execute(cmd);
			if(result.errors.length==0) {
				dialogModel.close("inserted");
			} else {
				dialogModel.clearMessages();
				for(var i=0; i<result.errors.length; i++) {
					var error = result.errors[i];
					dialogModel.addError(error);
				}
			}
		}
	}
	
	if(mode=="update") {
		var button = dialogModel.getToolbar().createButton();
		button.setTitle("Update");
		button.onClick = function() {
			var cmd = new Object();
			cmd.operation = "datastore.update";
			cmd.entity = new Object();
			cmd.entity.type = config.type;

			cmd.entity.key = dialogModel.getView().keyText.getValue();
			
			config.fillEntityFromDialog(dialogModel.getView(), cmd.entity);
			
			var result = WindData.execute(cmd);
			if(result.errors.length==0) {
				dialogModel.close("updated");
			} else {
				dialogModel.clearMessages();
				for(var i=0; i<result.errors.length; i++) {
					var error = result.errors[i];
					dialogModel.addError(error);
				}
			}
		}
	}
	
	{
		var button = dialogModel.getToolbar().createButton();
		button.setTitle("Cancel");
		button.onClick = function() {
			dialogModel.close();
		}
	}
	
	return dialogModel;
}


